package com.module.base.widget.decoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * Author: yanxuwen
 * Date: 2023/5/17
 * Desc: 分割线
 */
class ItemDecoration(
    private val context: Context,
    private val dividerColor: Int, // 分割线的颜色资源ID
    private val dividerWidth: Int, // 相邻子项之间的间隙宽度（单位为像素）
    private val dividerMargin: Int = 0, // 分割线的左右间距或上下间距（单位为像素）
) : RecyclerView.ItemDecoration() {

    private val dividerPaint = Paint()

    init {
        dividerPaint.color = dividerColor
        dividerPaint.style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)

        val layoutManager = parent.layoutManager
        if (layoutManager != null && layoutManager is LinearLayoutManager) {
            val orientation = layoutManager.orientation

            val startX: Float
            val startY: Float
            val endX: Float
            val endY: Float

            if (orientation == RecyclerView.VERTICAL) {
                startX = parent.paddingLeft + dividerMargin.toFloat()
                endX = parent.width - parent.paddingRight - dividerMargin.toFloat()

                for (i in 0 until parent.childCount - 1) {
                    val child = parent.getChildAt(i)
                    val top = child.bottom.toFloat()
                    val bottom = top + dividerWidth.toFloat()
                    canvas.drawRect(startX, top, endX, bottom, dividerPaint)
                }
            } else if (orientation == RecyclerView.HORIZONTAL) {
                startY = parent.paddingTop + dividerMargin.toFloat()
                endY = parent.height - parent.paddingBottom - dividerMargin.toFloat()

                for (i in 0 until parent.childCount - 1) {
                    val child = parent.getChildAt(i)
                    val left = child.right.toFloat()
                    val right = left + dividerWidth.toFloat()
                    canvas.drawRect(left, startY, right, endY, dividerPaint)
                }
            }
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        val layoutManager = parent.layoutManager
        if (layoutManager != null && layoutManager is LinearLayoutManager) {
            val orientation = layoutManager.orientation
            val position = parent.getChildAdapterPosition(view)
            val itemCount = parent.adapter?.itemCount ?: 0

            if (orientation == RecyclerView.VERTICAL) {
                if (position < itemCount - 1) {
                    outRect.bottom = dividerWidth
                }
            } else if (orientation == RecyclerView.HORIZONTAL) {
                if (position < itemCount - 1) {
                    outRect.right = dividerWidth
                }
            }
        }
    }
}

